﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Data.SqlClient;
using FirebirdSql.Data.FirebirdClient;
namespace FBStockTransfer
{
    //Store all Api from Fishbowl document
    public class Fishbowl
    {
        public static string listCustomerName(string key)
        {
            return "<FbiXml><Ticket><Key>" + key + "</Key></Ticket><FbiMsgsRq><CustomerNameListRq></CustomerNameListRq></FbiMsgsRq></FbiXml>";
        }
        public static string CreateSaleOrder(SqlDataReader order, string key)
        {
            DataUtility utility = new DataUtility();

            return "<FbiXml><Ticket><Key>" + key + "</Key></Ticket><FbiMsgsRq><SOSaveRq><SalesOrder>" +
                  "<Salesman>" + "admin" + "</Salesman>" +
                //Need to update order number 
                   "<Number>" + int.Parse(order["order_item_delivery_invoice_number"].ToString()) + "</Number>" +
                   "<Status>20 </Status>" +
                //"<Carrier></Carrier>" +
                  //"<FirstShipDate>"+ utility.FormatDate(order["order_item_delivery_date"].ToString()) +"</FirstShipDate>" +
                   "<CreatedDate>" + utility.FormatDate(order["order_date"].ToString()) + "</CreatedDate>" +
                   "<IssuedDate>" + utility.FormatDate(order["order_date"].ToString()) + "</IssuedDate>" +
                   "<TaxRateName>GST</TaxRateName>"+
                   "<CustomerContact>" + "Virgin Blue" + "</CustomerContact>" +
                   "<CustomerName>Virgin Blue</CustomerName>" +
                   "<FOB>" + "Origin" + "</FOB>" +
                   "<QuickBooksClassName></QuickBooksClassName>" +
                   "<LocationGroup>" + utility.LocationGroup(order["order_item_delivery_location"].ToString()) + "</LocationGroup>" +
                //"<LocationGroup>"+order["order_item_delivery_location"].ToString()+"</LocationGroup>" +
                   "<BillTo>" +
                   "<Name>" + "Virgin Blue" + "</Name>" +
                   "</BillTo>" +
                   "<Ship>" +
                   "<Name>Virgin Blue</Name>" +
                   "</Ship>" +
                   utility.returnOrderitems(int.Parse(order["order_item_delivery_invoice_number"].ToString())) +
                   "</SalesOrder><IssueFlag>true</IssueFlag></SOSaveRq></FbiMsgsRq></FbiXml>";
        }
        public static string CreateSaleOrder1(SqlDataReader order, string key)
        {
            DataUtility utility = new DataUtility();
            string InvoiceDate = order["deliverydate"].ToString();
            int DeliveryType = int.Parse(order["order_item_delivery_type"].ToString());
            int dayDelivery = DateTime.Parse(InvoiceDate.ToString()).Day;
            int monthDelivery = DateTime.Parse(InvoiceDate.ToString()).Month;
            int yearDelivery = DateTime.Parse(InvoiceDate.ToString()).Year;
            string locationGroup = order["order_item_delivery_location"].ToString();
            return "<FbiXml><Ticket><Key>" + key + "</Key></Ticket><FbiMsgsRq><SOSaveRq><SalesOrder>" +
                 "<Salesman>" + "admin" + "</Salesman>" +
                //Need to update order number 
                   "<Number>" + utility.ContractInvNum(InvoiceDate, DeliveryType, locationGroup).ToString() + "</Number>" +
                //"<Number>" + order["deliverydate"].ToString() + "</Number>" +
                   "<Status>20 </Status>" +
                //"<Carrier></Carrier>" +
                //"<FirstShipDate></FirstShipDate>" +

                   "<CreatedDate>" + InvoiceDate + "</CreatedDate>" +
                   "<IssuedDate>" + InvoiceDate + "</IssuedDate>" +
                   "<TaxRateName>GST</TaxRateName>"+
                   "<CustomerContact>" + "Virgin Blue" + "</CustomerContact>" +
                   "<CustomerName>Virgin Blue</CustomerName>" +
                   "<FOB>" + "Origin" + "</FOB>" +
                   "<QuickBooksClassName></QuickBooksClassName>" +
                   "<LocationGroup>" + utility.LocationGroup(locationGroup) + "</LocationGroup>" +
                //"<LocationGroup>"+order["order_item_delivery_location"].ToString()+"</LocationGroup>" +
                   "<BillTo>" +
                   "<Name>" + "Virgin Blue" + "</Name>" +
                   "</BillTo>" +
                   "<Ship>" +
                   "<Name>Virgin Blue</Name>" +
                   "</Ship>" +
                   utility.returnOrderitems1(dayDelivery, monthDelivery, yearDelivery, DeliveryType, locationGroup) +
                   "</SalesOrder><IssueFlag>true</IssueFlag></SOSaveRq></FbiMsgsRq></FbiXml>";
        }
        
        public static string GetPick(string key, string SOnumber)
        {
            return "<FbiXml><Ticket><Key>" + key + "</Key></Ticket><FbiMsgsRq><GetPickRq><PickNum>T" + SOnumber +
                   "</PickNum></GetPickRq></FbiMsgsRq></FbiXml>";
        }
        public static string SavePick(string key, string pick)
        {
            return "<FbiXml><Ticket><Key>" + key + "</Key></Ticket><FbiMsgsRq><SavePickRq>" + pick +
                    "</SavePickRq></FbiMsgsRq></FbiXml>";
        }
        public static string SaveShip(string key, string SOnumber, string deliveryDate)
        {

            return "<FbiXml><Ticket><Key>" + key + "</Key></Ticket><FbiMsgsRq><ShipRq><ShipDate>" + deliveryDate + "</ShipDate><OrderNums><OrderNum>S" + SOnumber + "</OrderNum></OrderNums>" +
                   "</ShipRq></FbiMsgsRq></FbiXml>";
        }
        public static string GetShipList(string key, string SOnumber, int locationGroupId)
        {
            /*return "<FbiXml><Ticket><Key>" + key + "</Key></Ticket><FbiMsgsRq><GetShipListRq><StartRecord>0</StartRecord>" +
                   "<RecordCount>10</RecordCount><OrderNumber>" + SOnumber + "</OrderNumber><LocationGroup>" + locationGroupId + "</LocationGroup>" +
                   "</GetShipListRq></FbiMsgsRq></FbiXml>";*/
            return "<FbiXml><Ticket><Key>" + key + "</Key></Ticket><FbiMsgsRq><GetShipListRq><StartRecord>0</StartRecord>" +
                   "<RecordCount>10</RecordCount><OrderNumber>" + SOnumber + "</OrderNumber>" +
                   "</GetShipListRq></FbiMsgsRq></FbiXml>";
        }
        public static string GetShipment(string key, int shipId)
        {
            return "<FbiXml><Ticket><Key>" + key + "</Key></Ticket><FbiMsgsRq><GetShipmentRq><ShipmentID>" + shipId + "</ShipmentID>" +
                   "</GetShipmentRq></FbiMsgsRq></FbiXml>";
        }
        public static string Export(string key, string exportType)
        {
            return "<FbiXml><Ticket><Key>" + key + "</Key></Ticket><FbiMsgsRq><ExportRq><Type>" + exportType + "</Type></ExportRq></FbiMsgsRq></FbiXml>";
        }
        public static string ProductRQ(string key, string producNum)
        {
            return "<FbiXml><Ticket><Key>" + key + "</Key></Ticket><FbiMsgsRq><ProductQueryRq><ProductNum>" + producNum +
                   "</ProductNum></ProductQueryRq></FbiMsgsRq></FbiXml>";
        }
        public static string LocationList(string key)
        {
            return "<FbiXml><Ticket><Key>" + key + "</Key></Ticket><FbiMsgsRq><LocationListRq></LocationListRq></FbiMsgsRq></FbiXml>";
        }
        public static string GetSo(string key, string customerName)
        {
            return "<FbiXml><Ticket><Key>" + key + "</Key></Ticket><FbiMsgsRq><GetSOListRq><CustomerName>" + customerName + "</CustomerName><DateScheduledBegin>2012-06-06T00:00:00</DateScheduledBegin></GetSOListRq></FbiMsgsRq></FbiXml>";
            //return "<FbiXml><Ticket><Key>" + key + "</Key></Ticket><FbiMsgsRq><GetSOListRq><SONum>24</SONum></GetSOListRq></FbiMsgsRq></FbiXml>";
        }

        public static string CreateTransferOrder(SqlDataReader myReader, ConnectionObject connectionObject, string key)
        {
            DataUtility utility = new DataUtility();
             string From = utility.updateStockName(myReader["transfer_from"].ToString());
             string To = utility.updateStockName(myReader["transfer_to"].ToString());
             string receiptNum = myReader["transfer_receipt_num"].ToString();
             string TOnumber = utility.TransferOrderName(From, To, receiptNum);
             string OrderDate = myReader["transfer_date"].ToString();

             return "<FbiXml><Ticket><Key>" + key + "</Key></Ticket><FbiMsgsRq><TransferOrderSaveRq>" +
                       "<TransferOrder>" +
                       "<Number>" + TOnumber + "</Number>" +
                       "<TypeID>20</TypeID>" +
                       "<Status>Issued</Status>" +
                       "<StatusID>20</StatusID>" +
                       "<ShipFromLG>" +
                       "<LocationGroup>" +
                       "<LocationGroupID>" + utility.LocationGroupId(From) + "</LocationGroupID>" +
                       "<LocationGroupName>" + From + "</LocationGroupName>" +
                       "<DefaultQBClassID>2</DefaultQBClassID>" +
                       "</LocationGroup>" +
                       "</ShipFromLG>" +
                       "<ShipToLG>" +
                       "<LocationGroup>" +
                       "<LocationGroupID>" + utility.LocationGroupId(To) + "</LocationGroupID>" +
                       "<LocationGroupName>" + To + "</LocationGroupName>" +
                       "<DefaultQBClassID>1</DefaultQBClassID>" +
                       "</LocationGroup>" +
                       "</ShipToLG>" +
                       "<OwnerIsFrom>true</OwnerIsFrom>" +
                       "<UserName>admin</UserName>" +
                       "<RevisionNumber>2</RevisionNumber>" +
                       "<DateScheduled>" + OrderDate + "T00:00:00" + "</DateScheduled>" +
                       "<Carrier>Will Call</Carrier>" +
                       "<CarrierID>1</CarrierID>" +
                       "<Note />" +
                       "<TransferOrderItems>" +
                       TransferOrderItem(myReader["transfer_from"].ToString(), myReader["transfer_to"].ToString(), OrderDate, receiptNum, TOnumber) +
                      "</TransferOrderItems>" +
                      "</TransferOrder><IssueFlag>true</IssueFlag>" +
                "</TransferOrderSaveRq></FbiMsgsRq></FbiXml>";
        }

        public static string TransferOrderItem(string from , string to, string date, string receipt_num, string toNumber)
        {
            int k = 1;
            DataUtility utility = new DataUtility();
            string transferOrderItemSql  = " select * from transfer_tbl where transfer_from = '" + from + "' "+
            " and transfer_to = '" + to + "' and CONVERT(VARCHAR(10), transfer_date, 23)='"+date+"' and transfer_receipt_num = " + receipt_num; 

            string TransferOrderItem = "";
            try
            {
                string VirginConnection = "user id=dinacorporate_vb;" + "password=pas5w0rd;server=122.201.105.167\\SQLSERVER;" + "database=dinacorporate_vb; " + "connection timeout=30;" + "Max Pool Size = 80";
                SqlConnection TransferOrderItemConnection = new SqlConnection(VirginConnection);
                TransferOrderItemConnection.Open();
                SqlCommand myCommand = new SqlCommand(transferOrderItemSql, TransferOrderItemConnection);
                SqlDataReader myReader = myCommand.ExecuteReader();
                while (myReader.Read())
                {
                    string upc = myReader["transfer_barcode"].ToString();
                    int quantity = int.Parse(myReader["transfer_qty"].ToString());
                    Product Product = utility.FindProduct(upc);
                    TransferOrderItem = TransferOrderItem +
                        "<TransferOrderItem>" +
                            "<TransferOrderItemID>" + receipt_num + "</TransferOrderItemID>" +
                            "<Type>Send</Type>" +
                            "<TypeID>10</TypeID>" +
                            "<Status>ENTERED</Status>" +
                            "<StatusID>10</StatusID>" +
                            "<Part>" +
                            "<PartID>"+Product.Product_PartID+"</PartID>" +
                            "<PartClassID>0</PartClassID>" +
                            "<TypeID>10</TypeID>" +

                            "<Num>"+Product.Product_Num+"</Num>" +
                            "<Description>"+Product.Product_Desc+"</Description>" +
                            "<Details />" +
                            "<StandardCost>0</StandardCost>" +
                            "<HasBOM>false</HasBOM>" +
                            "<Configurable>false</Configurable>" +
                            "<ActiveFlag>true</ActiveFlag>" +
                            "<SerializedFlag>false</SerializedFlag>" +
                            "<TrackingFlag>false</TrackingFlag>" +
                            "<Weight>0.01</Weight>" +

                            "<Width>0</Width>" +
                            "<Height>0</Height>" +
                            "<Len>0</Len>" +
                            "<UPC>"+upc+"</UPC>" +
                            "</Part>" +
                            "<UOM>" +
                            "<UOMID>1</UOMID>" +
                            "<Name>Each</Name>" +
                            "<Code>ea</Code>" +
                            "<Integral>true</Integral>" +
                            "<Active>true</Active>" +
                            "<Type>Count</Type>" +
                            "<UOMConversions>" +
                            "<UOMConversion>" +
                            "<MainUOMID>1</MainUOMID>" +
                            "<ToUOMID>17</ToUOMID>" +
                            "<ToUOMCode>pr</ToUOMCode>" +
                            "<ConversionMultiply>1.0</ConversionMultiply>" +
                            "<ConversionFactor>2.0</ConversionFactor>" +
                            "<ToUOMIsIntegral>false</ToUOMIsIntegral>" +
                            "</UOMConversion>" +
                            "<UOMConversion>" +
                            "<MainUOMID>1</MainUOMID>" +
                            "<ToUOMID>18</ToUOMID>" +
                            "<ToUOMCode>pk</ToUOMCode>" +
                            "<ConversionMultiply>1.0</ConversionMultiply>" +
                            "<ConversionFactor>20.0</ConversionFactor>" +
                            "<ToUOMIsIntegral>false</ToUOMIsIntegral>" +
                            "</UOMConversion>" +
                            "</UOMConversions>" +
                            "</UOM>" +
                            "<LineItem>"+k+"</LineItem>" +
                            "<LineNumber>"+Product.Product_Num+"</LineNumber>" +
                            "<LineDescription>"+Product.Product_Desc+"</LineDescription>" +
                            "<Note />" +
                            "<UnitCost>0</UnitCost>" +
                            "<RevisionNumber>1</RevisionNumber> " +
                            "<QuantityToFulfill>" + quantity + "</QuantityToFulfill>" +
                            "<QuantityFulfilled>0</QuantityFulfilled>" +
                            "<QuantityPicked>0</QuantityPicked>" +
                            "<DateScheduled>"+date+"T0:00:00"+"</DateScheduled>" +
                        "</TransferOrderItem>";
                    k++;
                }
                myReader.Close();
                TransferOrderItemConnection.Close();
                  }
            
            catch (Exception e)
            {
                Console.WriteLine(e.Message.ToString() + " " + toNumber);
            }
        
            return TransferOrderItem;
        }
    }
}
